package org.mspring.mlog.scheduler.job;

import java.io.File;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.transform.Transformers;
import org.mspring.mlog.ServiceFactory;
import org.mspring.mlog.entity.Post;
import org.mspring.mlog.service.PostService;
import org.mspring.mlog.utils.PostUrlUtils;
import org.mspring.mlog.utils.WebUtils;
import org.mspring.platform.config.ConfigService;
import org.mspring.platform.support.sitemap.ChangeFreq;
import org.mspring.platform.support.sitemap.GoogleCodeSitemapGenerator;
import org.mspring.platform.support.sitemap.GoogleCodeSitemapUrl;
import org.mspring.platform.support.sitemap.GoogleCodeSitemapUrl.FileType;
import org.mspring.platform.support.sitemap.GoogleMobileSitemapGenerator;
import org.mspring.platform.support.sitemap.GoogleMobileSitemapUrl;
import org.mspring.platform.support.sitemap.WebSitemapGenerator;
import org.mspring.platform.support.sitemap.WebSitemapUrl;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Gao Youbo
 * @since 2013-1-25
 */
public class SitemapJob extends BaseJob {

    private static final Logger log = LoggerFactory.getLogger(SitemapJob.class);

    @Override
    public void nativeExecuteInternal(JobExecutionContext context) {
        try {
            ConfigService configService = ServiceFactory.getConfigService();
            PostService postService = ServiceFactory.getPostService();
            String baseUrl = configService.getConfig("siteurl");
            String baseDirPath = WebUtils.getRealPath();
            File baseDir = new File(baseDirPath);

            WebSitemapGenerator generator = WebSitemapGenerator.builder(baseUrl, baseDir).fileNamePrefix("sitemap").build();
            GoogleCodeSitemapGenerator googleGenerator = GoogleCodeSitemapGenerator.builder(baseUrl, baseDir).fileNamePrefix("sitemap_google").build();
            GoogleMobileSitemapGenerator googleMobileGenerator = GoogleMobileSitemapGenerator.builder(baseUrl, baseDir).fileNamePrefix("sitemap_google_mobile").build();

            int batch = 100;
            int index = 0;
            long maxId = 0;
            while (true) {
                DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Post.class);
                ProjectionList pList = Projections.projectionList();
                pList.add(Projections.property("id").as("id"));
                pList.add(Projections.property("isTop").as("isTop"));
                pList.add(Projections.property("modifyTime").as("modifyTime"));
                detachedCriteria.setProjection(pList);
                detachedCriteria.setResultTransformer(Transformers.aliasToBean(Post.class));
                // detachedCriteria.addOrder(Order.asc("id"));
                // detachedCriteria.add(Restrictions.gt("id", maxId));
                List<Post> posts = postService.list(detachedCriteria, index, batch);

                for (Post post : posts) {
                    String loc = PostUrlUtils.getPostAbsUrl(post.getId());
                    double priority = 0.9;
                    if (post.getIsTop()) {
                        priority = 1.0;
                    }
                    WebSitemapUrl url = new WebSitemapUrl.Options(loc).lastMod(post.getModifyTime()).priority(priority).changeFreq(ChangeFreq.DAILY).build();
                    generator.addUrl(url);

                    // google
                    GoogleCodeSitemapUrl googleUrl = new GoogleCodeSitemapUrl.Options(loc, FileType.HTML).lastMod(post.getModifyTime()).priority(priority).changeFreq(ChangeFreq.DAILY).build();
                    googleGenerator.addUrl(googleUrl);

                    // google mobile
                    GoogleMobileSitemapUrl googleMobileSitemapUrl = new GoogleMobileSitemapUrl.Options(loc).lastMod(post.getModifyTime()).priority(priority).changeFreq(ChangeFreq.DAILY).build();
                    googleMobileGenerator.addUrl(googleMobileSitemapUrl);

                    if (post.getId() > maxId) {
                        maxId = post.getId();
                    }
                }

                if (posts.size() < batch) {
                    break;
                }
                index = index + batch;
            }

            generator.write();
            googleGenerator.write();
            googleMobileGenerator.write();

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
